package th.kmutt.se.is;

import th.kmutt.se.is.fact.adapter.RoomAdapter;
import th.kmutt.se.is.model.Room;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

public class RoomList extends Fragment implements OnClickListener, OnItemClickListener{

	Room roomListData[], room;

	View view;
	Button pageTitle;
	ListView roomList;
	ImageButton btnMenu1, btnMenu2;
	
	private String[] roomStatusText;
	private static int viewType = 0;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.screen_room_list, container, false);
		init();
		return view;
	}
	
	private void init(){
		
		room = new Room(getActivity());
		roomStatusText = getActivity().getResources().getStringArray(R.array.room_list);
		
		pageTitle = (Button) view.findViewById(R.id.pageTitle);
		btnMenu1 = (ImageButton) view.findViewById(R.id.btnMenu1);
		btnMenu2 = (ImageButton) view.findViewById(R.id.btnMenu2);
		roomList = (ListView) view.findViewById(R.id.roomList);
		
		pageTitle.setOnClickListener(this);
		btnMenu1.setOnClickListener(this);
		btnMenu2.setOnClickListener(this);
		roomList.setOnItemClickListener(this);
		
		process();
	}

	private void process(){
		
		roomListData = room.getRoomList(viewType);
		if(roomListData!=null){
			RoomAdapter adapter = new RoomAdapter(getActivity(), R.layout.row_rooms, roomListData);
			roomList.setAdapter(adapter);
			pageTitle.setText(roomStatusText[viewType]);
		}else{
			roomList.setAdapter(null);
		}
		
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if(requestCode==100)process();
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.pageTitle:{
				AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
				
				b.setItems(roomStatusText, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						viewType = which;
						process();
					}
				});
				b.create().show();
			}break;
			case R.id.btnMenu1:{
				((Main)getActivity()).openDrawer();
			}break;
			case R.id.btnMenu2:{
				Intent intent = new Intent(getActivity(), RoomAdd.class);
				startActivityForResult(intent, 100);
			}break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(getActivity(), RoomDetail.class);
		intent.putExtra("roomID", roomListData[arg2].getId()); //Room ID;
//		intent.putExtra("status", roomListData[arg2].getStatus());
		startActivity(intent);
	}
	
}
